package cn.dansj.controller;

import cn.dansj.common.request.annotation.http.GetMethod;
import cn.dansj.common.request.annotation.http.PostMethod;
import cn.dansj.common.request.annotation.http.RequestMappingMethod;
import cn.dansj.common.request.annotation.http.RequestParam;
import cn.dansj.common.utils.json.DictMap;
import cn.dansj.common.utils.bean.Return;
import cn.dansj.entity.WebCollect;
import cn.dansj.service.WebCollectService;
import cn.dansj.common.redis.RedisUtils;
import cn.dansj.common.utils.transfer.DateTime;
import cn.dansj.common.utils.transfer.GetTime;
import cn.dansj.common.utils.transfer.RandomUtils;
import cn.dansj.common.utils.transfer.Verification;
import org.springframework.data.domain.Page;

import java.sql.Timestamp;
import java.util.List;

@RequestMappingMethod("/webCollect")
public class WebCollectController {
    private final WebCollectService webCollectService;
    private final RedisUtils redisUtils;

    public WebCollectController(WebCollectService webCollectService, RedisUtils redisUtils) {
        this.webCollectService = webCollectService;
        this.redisUtils = redisUtils;
    }

    @GetMethod("/getData")
    public Return<?> getData(@RequestParam String url, @RequestParam int limit, @RequestParam int page) {
        Page<WebCollect> webCollectPage = webCollectService.search(page, limit, url);
        return Return.T(webCollectPage.getContent()).setTotalRecord(webCollectPage.getTotalElements());
    }

    @PostMethod("/save")
    public Return<?> save(@RequestParam WebCollect webCollect) {
        final Timestamp now = GetTime.dbTimestamp();
        if (Verification.checkNull(webCollect.getSequence())) {
            webCollect.setSequence(RandomUtils.sequence());
            webCollect.setCrtTime(now);
        }
        webCollect.setUpdateTime(now);
        webCollectService.save(webCollect);

        cacheData();
        return Return.T(true, "保存成功");
    }

    @PostMethod("/delete")
    public Return<?> delete(@RequestParam String sequence) {
        webCollectService.delete(sequence);
        return Return.T(true, "删除成功");
    }

    @PostMethod("/cache")
    public Return<?> cache() {
        return Return.T(cacheData());
    }

    @GetMethod("/get")
    public Return<?> getData() {
        final Object data = redisUtils.hget("webCollectData", "data");
        if (data == null) {
            final List<WebCollect> dbData = webCollectService.getAllData();
            redisUtils.hmset("webCollectData", DictMap.of("data", dbData, "cacheTime", DateTime.getInstance().stringTimestamp()));
            return Return.T(dbData);
        }
        return Return.T(DictMap.parseObject(data, new DictMap.JavaType<List<?>>() {
        }));
    }

    private int cacheData() {
        final List<WebCollect> data = webCollectService.getAllData();
        redisUtils.hset("webCollectData", "data", data);
        redisUtils.hset("webCollectData", "cacheTime", DateTime.getInstance().stringTimestamp());
        return data.size();
    }
}
